var AutoSize = new Class({
    initialize: function(elements, hw) {
        this.elements = $$(elements);
        this.doAuto(hw);
    },
    doAuto: function(hw) {
        if (!hw) {
            hw = 'height';
        }
        var max = 0,
            prop = (!Browser.ie6 ? 'min-': '') + hw, //ie6 ftl
            offset = 'offset' + hw.capitalize();
        this.elements.each(function(element, i) {
            var calc = element[offset];
            if (calc > max) {
                max = calc;
            }
        }, this);
        this.elements.each(function(element, i) {
            element.setStyle(prop, max - (element[offset] - element.getStyle(hw).toInt()));
        });
        return max;
    }
});

var Dialog = function(url,obj){
    var winSize = window.getSize();
    var bodySize = window.getScrollSize();
    var self = this;
    this.options = {
        url : url,
        method : 'post', 
        data:'',         
        width:540,       
        height:'auto',   
        title:'&nbsp'
    };
    var init = function(){
        if(obj){
            self.options = Object.merge(self.options,obj);
        }
        applyElement();
        ajaxLoad(self.options.url);
    }
    this.dialogEle = new Element('div',{
        'class':'dialog'
    });
    this.contentEle = new Element('div',{
        'class':'dialog-content'
    });
    this.headEle = new Element('div',{
        'class':'dialog-head'
    });
    this.closeEle = new Element('a',{
        'href':'#',
        'class':'close fixpng',
        'title':'关闭'
    });
    this.close = function(){
        self.dialogEle.destroy()
        self.mask.setStyle('display','none');
    };
    var applyElement = function(){
        if($('dialog_mask')){
            self.mask = $('dialog_mask');
        }else{
            self.mask = new Element('div',{
                'id':'dialog_mask'
            });
            self.mask.inject(document.body);
        }
        var maskHeight = Math.max(winSize.y,bodySize.y);
        self.mask.setStyles({
            'position':'absolute',
            'top':0,
            'left':0,
            'width':winSize.x,
            'height':maskHeight,
            'z-index':65534,
            'background':'#000000',
            'opacity':0.5,
            'display':''
        });
        self.closeEle.addEvent('click',function(e){
            e.stop();
            self.close();
        });
        self.headEle.set('html',self.options.title).inject(self.dialogEle);
        self.closeEle.inject(self.headEle);
        self.contentEle.inject(self.dialogEle);
        self.dialogEle.inject(document.body);
        self.style();
    };
    this.style = function(){
        var left = (winSize.x - self.options.width)/2;
        var _top = (winSize.y - self.dialogEle.getSize().y)/2 + $(document.body).getScroll().y;
        if(self.options.height != 'auto'){
            self.contentEle.setStyles({
                'height':self.options.height,
                'overflow-y':'auto'
            });
        }
        self.dialogEle.setStyles({
            'width':self.options.width,
            'position':'absolute',
            'left':left,
            'top':_top,
            'z-index':65535,
            'opacity':0
        });
        self.dialogEle.fade(1);
    }
    var ajaxLoad = function(url){
        new Request({
            'url':url,
            'evalScripts':true,
            'method' : self.options.method,
            'onComplete':function(re){
                self.contentEle.set('html',re);
                self.style();
            }
        }).send(self.options.data);
    }
    init();
}

var Loading = function (ele){
    var self = this;
    this.mask = new Element('div',{
        'class':'loading'
    });
    var _top = ele.getPosition().y;
    var left = ele.getPosition().x;
    var width = ele.getWidth();
    var height = ele.getHeight();
    this.mask.setStyles({
        'position':'absolute',
        'top':_top,
        'left':left,
        'width':width,
        'height':height,
        'z-index':65534
    });
    this.mask.inject($(document.body));
    this.close = function(){
        self.mask.destroy();
    }
}
